package com.yinhe.music.yhmusic.songmenu.presenter;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.log.BlankUtil;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.network.response.Response;
import com.yinhe.music.yhmusic.songmenu.MenuManagerActivity;
import com.yinhe.music.yhmusic.songmenu.contract.ISongMenuManagerContract;
import com.yinhe.music.yhmusic.utils.DataHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * {@link MenuManagerActivity} P层
 *
 * @author 何晓华
 */
public class SongMenuManagerPresenter extends BasePresenter<IBaseModel, ISongMenuManagerContract.ISongMenuManagerView> implements ISongMenuManagerContract.ISongMenuManagerPresenter {

    @Override
    public void getMenuList() {
        ISongMenuManagerContract.ISongMenuManagerView managerView = getView();

        if (managerView == null) {
            return;
        }

        List<SongMenuList> menuLists = DataHolder.getInstance().getData();
        managerView.setSongMenuListUI(menuLists);
    }

    @Override
    public void reqDelete(List<SongMenuList> lists, int menuType) {
        ISongMenuManagerContract.ISongMenuManagerView mView = getView();
        if (mView == null) {
            return;
        }

        if (BlankUtil.isBlank(lists)) {
            getView().showToast("没有可以删除的歌单");
            return;
        }

        mView.showLoading();

        List<SongMenuList> deleteList = new ArrayList<>();
        List<Integer> deleteIdList = new ArrayList<>();

        for (int i = 0; i < lists.size(); i++) {
            SongMenuList menu = lists.get(i);
            if (menu.isCheck()) {
                deleteList.add(menu);
                deleteIdList.add(menu.getSongMenuId());
            }
        }

        if (deleteList.isEmpty()) {
            getView().showToast("请选择要删除的歌单");
            return;
        }

        String idString = JsonUtil.getJsonArray(deleteIdList);

        if (menuType == IConstants.COLLECT_MENU) {
            Disposable disposable = mModel.batchUnCollection(idString, null, IConstants.SONGMENU)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> handleResponse(baseInfoResponse, deleteList, deleteIdList), throwable -> {
                        mView.hideLoading();
                        mView.showMessage(throwable);
                    });
            mDisposable.add(disposable);
        } else {
            Disposable disposable = mModel.deleteUserSongMenu(idString)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(
                        baseInfoResponse -> handleResponse(baseInfoResponse, deleteList, deleteIdList),
                        throwable -> {
                            mView.hideLoading();
                            mView.showMessage(throwable);
                    });
            mDisposable.add(disposable);
        }
    }

    private void handleResponse(Response baseInfoResponse, List<SongMenuList> deleteList, List<Integer> deleteIdList) {
        if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
            getView().updateSongMenuListUI(deleteList);
            getView().showToast(baseInfoResponse.getMsg());
            DataBaseAccessor.getInstance().deleteMenuListByIdSyn(deleteIdList);
            RxBus.get().post(RxBusEventType.SongMenu.DELETE_MENU, RxbusNullObject.INSTANCE);
        } else {
            getView().showToast(baseInfoResponse.getMsg());
        }
        getView().hideLoading();
    }
}
